package cn.blunce.view_utils.PopWindowHelper;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.lang.reflect.Method;

import cn.blunce.mylib.R;

public class PopWindowTest extends Activity implements View.OnClickListener, PopupWindow.OnDismissListener {

    private Button windowAction;
    private PopupWindow window;

    private boolean showWindows = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pop_window_test);

        windowAction = (Button) findViewById(R.id.window_action);
        windowAction.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.window_action:
                WindowsAction();
                break;
            default:
        }
    }

    private void WindowsAction() {
        if (showWindows) {
            //隐藏PopWindow
            window.dismiss();
        } else {
            //显示PopWindow
            showWindows = true;
//            initWindowCode();
            initWindowXml();
        }
    }


    private void initWindowCode() {
        TextView textView = new TextView(this);
        textView.setText("Hello world!");
        textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        window = new PopupWindow(textView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        window.showAsDropDown(windowAction);
        window.setOutsideTouchable(true);
        window.setOnDismissListener(this);
        setPopupWindowTouchModal(window, true);
    }

    private void initWindowXml() {
        View layout = LayoutInflater.from(this).inflate(R.layout.pop_window_layout, null);
        window = new PopupWindow(layout, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        window.showAsDropDown(windowAction);
        window.setOutsideTouchable(true);
        window.setOnDismissListener(this);
        setPopupWindowTouchModal(window, true);
    }


    @Override
    public void onDismiss() {
        showWindows = false;
    }

    /**
     * Set whether this window is touch modal or if outside touches will be sent
     * to
     * other windows behind it.
     */
    public static void setPopupWindowTouchModal(PopupWindow popupWindow,
                                                boolean touchModal) {
        if (null == popupWindow) {
            return;
        }
        Method method;
        try {

            method = PopupWindow.class.getDeclaredMethod("setTouchModal",
                    boolean.class);
            method.setAccessible(true);
            method.invoke(popupWindow, touchModal);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
